<template>
  <div class="body-container">
    <el-container class="my-define-el-container1" v-if="sceneType>0">
      <el-aside class="my-define-aside1">
        <sub-menu ref="subMenu" :sceneType="sceneType" :defaultSceneId="defaultSceneId" :pageTitle="pageTitle"
          :pageIcon="pageIcon"></sub-menu>
      </el-aside>
      <el-container class="my-define-el-container2">
        <el-main class="my-define-main2" v-if="currentView">
          <el-tabs class="my-top-tab" v-model="tabsValue" @tab-remove="removeTab">
            <el-tab-pane class="my-tab-item" v-for="(item, index) in menuTabs" :key="item.name" :name="item.name"
              :closable="item.closable">
              <span slot="label">
                <svg-icon :icon-class="pageIcon" />
                {{item.title}}
              </span>
            </el-tab-pane>
          </el-tabs>
          <div class="my-define-main2-view" v-loading="loading">
            <!-- <table-grid :reqData="reqData" :reqForm="reqForm" ref="tableGrid" :sceneType="sceneType" :sceneId="sceneId"
              :defineSceneId="defineSceneId">
            </table-grid> -->
            <div :reqData="reqData" :reqForm="reqForm" ref="tableGrid" :sceneType="sceneType" :sceneId="sceneId"
              :defineSceneId="defineSceneId" :is="currentView"></div>
          </div>
        </el-main>
        <el-main v-if="!currentView">
          <el-empty v-if="isEmptyData" description="暂无数据"></el-empty>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import subMenu from "@/views/master/Submenu";
  import tableGrid from "@/views/master/Table";
  import userGrid from "@/views/system/users/index";
  import roleGrid from "@/views/system/roles/index";
  import tenantGrid from "@/views/system/tenants/index";
  import departmentGrid from "@/views/system/department/index";
  import notifyGrid from "@/views/system/notify/index";
  import tableColumnGrid from "@/views/system/tablecolumn/index";
  import codeTemplateGrid from "@/views/system/dbtemplate/index";
  import menuGrid from "@/views/system/menu/index";
  import {
    queryList,
    queryModel,
    saveModel,
    removeModel,
    importModel,
    queryAuth
  } from '@/api/system/queryData'
  export default {
    name: 'task-main',
    components: {
      subMenu,
      tableGrid,
      userGrid,
      roleGrid,
      tenantGrid,
      departmentGrid,
      notifyGrid,
      tableColumnGrid,
      codeTemplateGrid,
      menuGrid
    },
    props: {
      sceneType: {
        type: Number,
        default: 0
      },
      pageTitle: {
        type: String,
        default: ''
      },
      pageIcon: {
        type: String,
        default: ''
      },
      defaultSceneId: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        tabsValue: '1',
        menuTabs: [],
        currentView: '',
        sceneId: 0,
        defineSceneId: 0,
        focusId: 0,
        loading: true,
        nextPageIndex: 1,
        currentTotalCount: 0,
        defaultPageSize: 100,
        reqData: {
          loading: true,
          // 返回结果集
          tableData: [],
          // 查询参数
          queryParams: {
            queryText: undefined,
            pageIndex: 1,
            pageSize: 100,
            orderBy: 'Id',
            sort: 'descending',
            queryData: '',
            sortData: '',
            groupData: '',
            sceneId: 0,
            defineSceneId: 0,
            sceneType: 0,
            focusId: 0
          },
          // 合计条数
          total: 0,
          //操作权限
          operation: {},
          rightMenuData: [],
          noMore: false
        },
        reqForm: {
          dialogWidth: '30%',
          currentView: 'masterForm',
          formData: {
            Id: 0
          },
          dialogShow: false,
          dialogTitle: '',
          roomIds: [],
          uploadFormData: {
            required: true,
            uploadFile: false,
            uploadFolder: 'RoomBasic',
            fileList: []
          },
          loading: false,
          denyEdit: false,
          recordData: []
        },
        allRightMenuData: [],
        isEmptyData: false
      }
    },
    created() {
      var that = this
    },
    watch: {
      tabsValue: {
        handler(newValue, oldValue) {
          if (newValue) {
            var tabItem = this.menuTabs.find((item) => {
              return item.name === newValue
            })
            if (tabItem) {
              // this.$refs.subMenu.doClickSceneProcess(null, tabItem.id)
            }
          }
        }
      },
      sceneType: {
        handler(newValue, oldValue) {
          this.menuTabs = []
          this.currentView = ''
        }
      }
    },
    methods: {
      getData() {
        var that = this
        this.reqData.loading = true
        if (this.reqData.queryParams.pageIndex == 1) {
          this.reqData.tableData = []
        }
        queryList(this.reqData.queryParams).then(res => {
          this.reqData.loading = false
          if (res.status) {
            if (this.reqData.queryParams.pageIndex == 1) {
              this.reqData.tableData = res.outputData.DataSource
            } else {
              this.reqData.tableData = this.reqData.tableData.concat(res.outputData.DataSource)
            }
            this.reqData.total = res.outputData.TotalCount
            this.reqData.noMore = this.reqData.tableData.length == this.reqData.total || this.reqData.total == 0
            this.currentTotalCount = this.reqData.tableData.length
            if (this.reqData.queryParams.pageIndex == 1) {
              this.$bus.$emit('CLEARSELECTIONIDS', this.defineSceneId)
            }
            // if (!this.reqData.noMore) {
            //   var winHeight = window.innerHeight - 180
            //   var rowHeight = this.$refs.tableGrid.rowStyle.height.replace('px', '')
            //   var tableHeight = this.reqData.tableData.length * Number(rowHeight) - 50
            //   if (winHeight > tableHeight) {
            //     this.nextPageIndex++
            //     this.reqData.queryParams.pageIndex = this.nextPageIndex
            //     this.getData()
            //   }
            // }
          }
          setTimeout(() => {
            that.getPermission()
          }, 100)
        })
      },
      doClickScene(data) {
        if (!data) {
          this.isEmptyData = true
          return
        }
        this.isEmptyData = false
        var that = this
        this.currentView = ''
        this.loading = true
        // this.sceneType = data.sceneType
        this.sceneId = data.sceneId
        this.defineSceneId = data.defineSceneId
        that.reqData.queryParams.sceneId = this.sceneId
        that.reqData.queryParams.defineSceneId = this.defineSceneId
        that.reqData.queryParams.sceneType = this.sceneType
        var tabItem = {
          id: data.id,
          title: data.label,
          name: data.defineSceneId + '',
          class: 'leftmenu-desktop',
          activeScene: '',
          closable: false,
          sceneType: data.sceneType,
          sceneId: data.sceneId,
          defineSceneId: data.defineSceneId
        }
        this.addTab(tabItem)
        this.focusId = 0
        this.$nextTick(() => {
          this.currentView = data.viewName || 'tableGrid'
          this.loading = false
        })
      },
      getPermission() {
        queryAuth({
          defineSceneId: this.defineSceneId
        }).then(res => {
          if (res.status) {
            this.reqData.operation = res.outputData.operation
            this.reqData.rightMenuData = []
            if (this.reqData.operation.edit) {
              this.reqData.rightMenuData.push({
                id: 1,
                icon: '',
                label: '编辑',
              })
            }
            if (this.reqData.operation.delete) {
              this.reqData.rightMenuData.push({
                id: 2,
                icon: '',
                label: '删除'
              })
            }
            if (this.reqData.operation.comment) {
              this.reqData.rightMenuData.push({
                id: 5,
                icon: '',
                type: 'comment',
                label: '评论'
              })
            }
            if (this.reqData.operation.remind) {
              this.reqData.rightMenuData.push({
                id: 6,
                icon: '',
                type: 'remind',
                label: '提醒'
              })
            }
            if (this.reqData.operation.lock) {
              this.reqData.rightMenuData.push({
                id: 7,
                icon: '',
                type: 'lock',
                label: '锁定'
              })
              this.reqData.rightMenuData.push({
                id: 8,
                icon: '',
                type: 'unlock',
                label: '解除锁定'
              })
            }
            this.allRightMenuData = this.reqData.rightMenuData
          }
        })
        this.reqData.operation = {
          add: true,
          edit: true,
          delete: true,
          tableView: true,
          focus: true,
          hide: true,
          query: true,
          share: true,
          print: true,
          sort: true,
          lineHeight: true,
          search: true,
          export: true,
          group: true,
          source: true,
          more: true,
          import: true,
          defineColumn: true
        }
      },
      doSearch(option) {
        if (option) {
          this.reqData.queryParams.queryText = ''
          if (option.queryText) {
            this.reqData.queryParams.queryText = option.queryText;
          }
          this.reqData.queryParams.queryData = ''
          if (option.queryData) {
            this.reqData.queryParams.queryData = JSON.stringify(option.queryData);
          }
          this.reqData.queryParams.sortData = ''
          if (option.sortData) {
            this.reqData.queryParams.sortData = JSON.stringify(option.sortData);
          }
          this.reqData.queryParams.focusId = 0
          if (option.focusId) {
            this.reqData.queryParams.focusId = option.focusId
          }
          this.reqData.queryParams.groupData = ''
          if (option.groupData) {
            this.reqData.queryParams.groupData = JSON.stringify(option.groupData);
          }
          if (option.goNext) {
            this.nextPageIndex++
          } else {
            this.nextPageIndex = 1
          }
          this.reqData.queryParams.pageIndex = this.nextPageIndex
          this.reqData.queryParams.pageSize = this.defaultPageSize
          this.getData()
        }
      },
      reloadList() {
        this.nextPageIndex = 1
        this.reqData.queryParams.pageIndex = 1
        this.reqData.queryParams.pageSize = this.currentTotalCount > 0 ? this.currentTotalCount + 1 : this
          .defaultPageSize
        this.getData()
      },
      getFormData() {
        queryModel({
          Id: this.reqForm.formData.Id,
          sceneType: this.sceneType,
          sceneId: this.sceneId,
          defineSceneId: this.defineSceneId
        }).then(res => {
          if (res.status) {
            this.reqForm.formData = res.outputData.model
            this.reqForm.denyEdit = res.outputData.operation.denyEdit
          }
        })
      },
      doAdd() {
        this.reqForm.dialogWidth = '70%'
        this.reqForm.formData.Id = 0;
        this.reqForm.dialogTitle = '新增'
        this.reqForm.dialogShow = true
        this.reqForm.currentView = 'masterForm'
        this.getFormData()
      },
      doEdit(Id) {
        this.reqForm.dialogWidth = '70%'
        this.reqForm.formData.Id = Id
        this.reqForm.dialogTitle = '编辑'
        this.reqForm.dialogShow = true
        this.reqForm.currentView = 'masterForm'
        this.getFormData()
      },
      doRemove(Ids) {
        if (Ids.length === 0) {
          return
        }
        this.$confirm(
            '是否确认删除选中的 ' + Ids.length + ' 条数据?',
            '警告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          )
          .then(() => {
            removeModel({
              Ids: JSON.stringify(Ids),
              defineSceneId: this.defineSceneId
            }).then(res => {
              if (res.status) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
                this.reloadList()
              }
            })
          })
      },
      doClosePop(reload) {
        this.reqForm.dialogShow = false
        if (reload) {
          this.reloadList();
        }
      },
      setRightMenu(row, remindColumn) {
        var that = this
        that.reqData.rightMenuData = []
        that.allRightMenuData.forEach(function(item) {
          // 计划编制
          // if (this.sceneId == 10 && item.id == 1) {
          //   return true
          // }
          if (item.type == 'remind') {
            if (!remindColumn || !remindColumn.showRemind) {
              return true
            }
          }
          if (item.type == 'lock') {
            if (row.isLocked) {
              return true
            }
          }
          if (item.type == 'unlock') {
            if (!row.isLocked) {
              return true
            }
          }
          if (row.isLocked && item.type != 'unlock') {
            return true
          }
          that.reqData.rightMenuData.push(item)
        })
      },
      doClickRightMenu(item, row, selectionIds) {
        if (item.id == 1) {
          this.doEdit(row.Id);
        }
        if (item.id == 2) {
          var Ids = [row.Id];
          if (selectionIds && selectionIds.length > 0) {
            Ids = selectionIds
          }
          this.doRemove(Ids);
        }
        if (item.id == 3) {
          var Ids = [row.Id];
          this.doMoveIn(Ids);
        }
        if (item.id == 4) {
          var Ids = [row.Id];
          this.doMoveOut(Ids);
        }
        if (item.id == 5) {
          var Ids = [row.Id];
          this.dpRate(Ids);
        }
        if (item.id == 6) {
          var Ids = [row.Id];
          this.dpComment(Ids);
        }
      },
      doImport() {
        this.reqForm.dialogWidth = '30%'
        this.reqForm.dialogTitle = '导入'
        this.reqForm.uploadFolder = 'Room'
        this.reqForm.dialogShow = true
        this.reqForm.currentView = 'masterImport'
      },
      //提交导入
      submitUpload(fileIds) {
        var that = this;
        this.reqForm.loading = true
        importModel({
          fileIds: fileIds,
          sceneId: this.sceneId,
          sceneType: this.sceneType,
          defineSceneId: this.defineSceneId
        }).then(res => {
          this.reqForm.loading = false
          if (res.status) {
            this.$message({
              message: '导入成功',
              type: 'success'
            })
            this.doClosePop(true)
          }
        })
      },
      doExport(Ids) {
        this.reqData.loading = true
        let postForm = this.reqData.queryParams
        postForm.export = true
        if (Ids.length == this.reqData.queryParams.pageSize || Ids.length == this.reqData.total) {
          postForm.exportIds = '[]'
        } else {
          postForm.exportIds = JSON.stringify(Ids)
        }
        queryList(postForm).then(res => {
          this.reqData.loading = false
          postForm.export = false
          if (res.status) {
            window.location.href = res.outputData.downloadUrl
          }
        })
      },
      doMoveIn(Ids) {
        this.reqForm.dialogWidth = '50%'
        this.reqData.roomIds = Ids
        this.reqForm.dialogTitle = '迁入'
        this.reqForm.dialogShow = true
        this.reqForm.currentView = 'moveIn'
      },
      doMoveOut(Ids) {
        this.reqForm.dialogWidth = '50%'
        this.reqData.roomIds = Ids
        this.reqForm.dialogTitle = '迁出'
        this.reqForm.dialogShow = true
        this.reqForm.currentView = 'moveOut'
      },
      doPrint(Ids) {
        this.reqForm.dialogWidth = '30%'
        this.reqForm.dialogTitle = '打印'
        this.reqForm.dialogShow = true
        this.reqForm.currentView = 'printChoose'
      },
      addTab(item) {
        this.menuTabs = []
        var index = this.menuTabs.findIndex((menu) => {
          return menu.name == item.name
        })
        if (index > -1) {
          this.tabsValue = this.menuTabs[index].name
        } else {
          this.menuTabs.push(item)
          this.tabsValue = item.name
        }
      },
      getIconClass(item) {
        if (this.tabsValue == item.name) {
          return item.class + '-active'
        }
        return item.class
      },
      removeTab(targetName) {
        var tabIndex = this.menuTabs.findIndex((menu) => {
          return menu.name === targetName
        })
        this.menuTabs.splice(tabIndex, 1)
        if (this.menuTabs.length > 0) {
          this.tabsValue = this.menuTabs[this.menuTabs.length - 1].name
        } else {
          this.tabsValue = 0
        }
      },
      //流程管理
      doEditFlow() {
        this.reqForm.dialogWidth = '70%'
        this.reqForm.formData.Id = 0
        this.reqForm.dialogTitle = '流程管理'
        this.reqForm.dialogShow = true
        this.reqForm.currentView = 'flowIndex'
      },
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
